// pages/good/goods/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    tab: [
      "全部",
      "仅标题",
      "仅内容"
    ],
    searchKey: '',
    allList: [],
    list: [
      {
        id: 1,
        goodName: "b站大会员满减券",
        goodCarbonCurrency: 200,
        imagePath: "../../../image/flower.png",
        goodType: 1
      }
    ]
  },

  /**
   * 切换
   */
  onChange(event) {
    console.log(event.detail)

    this.setData({
      active: event.detail.index
    })

    if(event.detail.index == 0){
      this.setData({
        list: this.data.allList[0]
      })
    }else if(event.detail.index == 1){
      this.setData({
        list: this.data.allList[1]
      })
    }else{
      this.setData({
        list: this.data.allList[2]
      })
    }
  },

  /**
   * 获取所有商品
   */
  getGood() {
    var that = this

    wx.request({
      url: app.globalData.url + '/search_good/',
      method: "GET",
      data: {
        searchContent: that.data.searchKey
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)

        if(res.data.status_code == 500){
          wx.showToast({
            title: '获取失败',
            duration: 2000,
            icon: 'error'
          })
        }else {
          that.setData({
            allList: res.data,
            list: res.data[0]
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '连接失败',
          duration: 2000,
          icon: 'error',
        })
      }
    })
  },

  /**
   * 跳转详情页
   */
  toDetailsTap: function(e) {
    console.log(e.currentTarget.dataset.id);

    wx.navigateTo({
      url: "../detail/index?id=" + e.currentTarget.dataset.id
    })
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.searchKey)
    this.setData({
      searchKey: options.searchKey
    })
    this.getGood()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})